package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Leave;
import cn.wolfcode.car.business.domain.ServiceItemAuditVO;
import cn.wolfcode.car.business.mapper.LeaveMapper;
import cn.wolfcode.car.business.query.LeaveQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ILeaveService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class LeaveServiceImpl implements ILeaveService {
    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private RuntimeService runtimeService;
    @Override
    public TablePageInfo<Leave> query(LeaveQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Leave>(leaveMapper.selectForList(qo));
    }


    @Override
    public void startAudit(ServiceItemAuditVO vo,Leave leave) {
        leave.setAuditid(vo.getDirector().intValue());
        leave.setStatus("1");
        leaveMapper.insert(leave);

        Map<String, Object> map = new HashMap<>();
        if(vo.getDirector() != null){
            map.put("director", vo.getDirector().toString());
        }
        if(vo.getFinance() != null){
            map.put("finance", vo.getFinance().toString());
        }

        BpmnInfo bpmnInfo = bpmnInfoService.get(vo.getBpmnInfoId());
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(
                bpmnInfo.getActProcessKey(),
                map
        );
    }


}
